<?php
namespace app\admin\controller;
use app\admin\controller\Permissions;
use think\Config;
use \think\Db;
use \think\Cookie;
use \think\Session;
use think\Request;

use \think\Cache;
class Chaiqian extends Permissions
{
    public function chaiqian()


    {
        $request = Request::instance();

    	  $zhu['all'] = db('population')->where(['isdel'=>1])->count('id'); //全部
        $zhu['yiwancheng'] = db('population')->where(['isdel'=>1,'status'=>1])->count('id'); //已完成
        $zhu['yiqian'] = db('population')->where(['isdel'=>1,'status'=>2])->count('id'); //已签
        $zhu['yilianxi'] = db('population')->where(['isdel'=>1,'status'=>3])->count('id'); //已联系
        $zhu['weilianxi'] = db('population')->where(['isdel'=>1,'status'=>4])->count('id'); //未联系
        // switch ($zhu['yiwancheng']) {
        //     case 'value':
        //         # code...
        //         break;
            
        //     default:
        //         # code...
        //         break;
        // }
        $zhu['bl1'] = ($zhu['yiwancheng']/$zhu['all'])*100;
        $zhu['bl2'] = ($zhu['yiqian']/$zhu['all'])*100;
        $zhu['bl3'] = ($zhu['yilianxi']/$zhu['all'])*100;
        $zhu['bl4'] = ($zhu['weilianxi']/$zhu['all'])*100;
        $city = $request->get();

        $statusType = input('statusType');


        if(!$statusType) {
            if (count($city) > 0 && $city['city'] > 0) {
                $list = db('population')->where(['isdel' => 1])->where(['status' => $city['city']])->select();
            } else {
                $list = db('population')->where(['isdel' => 1])->select();
            }
        }else{
            $list = db('population')->where(['isdel' => 1])->where(['status' => $statusType])->select();
        }
        //户主

        foreach ($list as $k => &$v) {
            switch ($v['status']) {
                case '1':
                    $v['status'] = '已完成';
                    break;
                case '2':
                    $v['status'] = '已签';
                    break;
                case '3':
                    $v['status'] = '已联系';
                    break;
                case '4':
                    $v['status'] = '未联系';
                    break;
                default:
                    break;
            }
            $dishang = db('dishang')->where(['uid'=>$v['id']])->sum('sum');
            $gexiang = db('gexiang')->where(['uid'=>$v['id']])->sum('sum');

            $ewai = db('ewai')->where(['uid'=>$v['id']])->sum('sum');
            $v['chaiqianbu'] = $dishang+$gexiang+$ewai;

            $v['hqjiesuan'] = db('hqjiesuan')->where(['uid'=>$v['id']])->sum('jinesum');

            $v['zong'] = $v['chaiqianbu']+$v['hqjiesuan'];

            $louhao = explode(',',$v['louhao']);

            foreach ($louhao as &$vv) {
                $vv = db('louhao')->where(['id'=>$vv])->value('louhao');
            }
            // dump($louhao);die;
            $v['louhao'] = implode(',',$louhao);
        }
       
        $this->assign('list',$list);
        $this->assign('zhu',$zhu);
        $this->assign('city', $city);


        $admin_cate_id = Session::get('admin_cate_id');
        $this->assign('admin_cate_id',$admin_cate_id);
        $operation = Config::get('operation');
        $this->assign('operation',$operation);

        return $this->fetch();
    }
}
